package cn.yx.common.security.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.yx.common.core.constant.NumberConstant;
import cn.yx.common.core.util.date.DateUtil;
import cn.yx.common.security.pojo.JwtUserDetails;
import cn.yx.permission.remote.service.RoleUserRemoteService;
import cn.yx.user.remote.dto.UserAccountRemoteDto;
import cn.yx.user.remote.service.UserAccountRemoteService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>SpringSecurity UserDetailsService用户服务接口实现类</p>
 *
 * @author Wgssmart
 */
@Component
public class JwtUserDetailsServiceImpl implements JwtUserDetailsService {

    @DubboReference(check = false)
    private UserAccountRemoteService userAccountRemoteService;

    @DubboReference(check = false)
    private RoleUserRemoteService roleUserRemoteService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserAccountRemoteDto userAccountRemoteDto = userAccountRemoteService.getByUserName(username);
        if (Objects.nonNull(userAccountRemoteDto)) {
            // 填充基础信息到jwtUserDetails
            JwtUserDetails jwtUserDetails = fillBasicInfo(username, userAccountRemoteDto);
            // 填充角色信息到jwtUserDetails
            fillAuthorityInfo(jwtUserDetails);
            return jwtUserDetails;
        }
        JwtUserDetails jwtUserDetails = new JwtUserDetails();
        jwtUserDetails.setId(NumberConstant.LONG_ZERO);
        return jwtUserDetails;
    }

    /**
     * 获取JwtUserDetails对象
     *
     * @param username
     * @param userAccountRemoteDto
     */
    private JwtUserDetails fillBasicInfo(String username, UserAccountRemoteDto userAccountRemoteDto) {
        return new JwtUserDetails(userAccountRemoteDto.getId(), username,
                userAccountRemoteDto.getAccount(), userAccountRemoteDto.getPhone(),
                userAccountRemoteDto.getEmail(), null, null, DateUtil.date());
    }

    /**
     * 填充角色信息到jwtUserDetails
     *
     * @param jwtUserDetails
     */
    private void fillAuthorityInfo(JwtUserDetails jwtUserDetails) {
        List<String> roleList = roleUserRemoteService.listRolesByUserId(jwtUserDetails.getId());
        if (CollectionUtil.isNotEmpty(roleList)) {
            List<SimpleGrantedAuthority> simpleGrantedAuthorityList = new ArrayList<>();
            for (String role : roleList) {
                simpleGrantedAuthorityList.add(new SimpleGrantedAuthority(role));
            }
            jwtUserDetails.setAuthorities(simpleGrantedAuthorityList);
        }
    }

    @Override
    public Mono<UserDetails> findByUsername(String username) {
        UserAccountRemoteDto userAccountRemoteDto = userAccountRemoteService.getByUserName(username);
        if (Objects.nonNull(userAccountRemoteDto)) {
            // 填充基础信息到jwtUserDetails
            JwtUserDetails jwtUserDetails = fillBasicInfo(username, userAccountRemoteDto);
            // 填充角色信息到jwtUserDetails
            fillAuthorityInfo(jwtUserDetails);
            return Mono.just(jwtUserDetails);
        }
        JwtUserDetails jwtUserDetails = new JwtUserDetails();
        jwtUserDetails.setId(NumberConstant.LONG_ZERO);
        return Mono.just(jwtUserDetails);
    }
}
